package com.pshcp.doctor.api;

import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.pshcp.doctor.ao.assistantsettings.SaveDoctorAssistantSettingsAO;
import com.pshcp.doctor.tool.DoctorAssistantSettingsTool;
import com.pshcp.doctor.vo.assistantsettings.DoctorAssistantSettingsVO;
import com.pshcp.domain.model.DoctorAssistantSettings;
import com.pshcp.service.DoctorAssistantSettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/3 17:53
 */
@Api(tags = {"医生端-助手设置接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/assistantSettings")
public class DoctorAssistantSettingsAPI {

    @Autowired
    private DoctorAssistantSettingsService doctorAssistantSettingsService;

    @ApiOperation("获取医生助手设置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取医生助手设置")
    @PostMapping("getDoctorAssistantSettings")
    public ResultDTO<DoctorAssistantSettingsVO> getDoctorAssistantSettings() throws BizException {
        DoctorAssistantSettings doctorAssistantSettings = doctorAssistantSettingsService.getDoctorAssistantSettingsByDoctorId(ReqComTool.getUserId());
        DoctorAssistantSettingsVO doctorAssistantSettingsVO = DoctorAssistantSettingsTool.buildGetDoctorAssistantSettingsVO(doctorAssistantSettings);
        return ResultBuilder.success(doctorAssistantSettingsVO);
    }

    @ApiOperation("保存医生助手设置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "保存医生助手设置")
    @PostMapping("saveDoctorAssistantSettings")
    public ResultDTO<Long> saveDoctorAssistantSettings(@RequestBody SaveDoctorAssistantSettingsAO saveDoctorAssistantSettingsAO) throws BizException {
        DoctorAssistantSettings doctorAssistantSettings = doctorAssistantSettingsService.getDoctorAssistantSettingsByDoctorId(ReqComTool.getUserId());
        if (doctorAssistantSettings == null) {
            DoctorAssistantSettings saveDoctorAssistantSettings = DoctorAssistantSettingsTool.buildSaveDoctorAssistantSettings(saveDoctorAssistantSettingsAO);
            doctorAssistantSettingsService.insert(saveDoctorAssistantSettings);
        } else {
            DoctorAssistantSettings updateDoctorAssistantSettings = DoctorAssistantSettingsTool.buildSaveDoctorAssistantSettings(doctorAssistantSettings, saveDoctorAssistantSettingsAO);
            doctorAssistantSettingsService.update(updateDoctorAssistantSettings);
        }
        return ResultBuilder.success(null);
    }
}
